package com.it.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.commonutils.R;
import com.it.eduservice.entity.EduTeacher;
import com.it.eduservice.entity.vo.TeacherQuery;
import com.it.eduservice.service.EduTeacherService;
import com.it.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author test.java
 * @since 2021-01-04
 */
@Api(tags="讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
//@CrossOrigin //解决跨域
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    //1 增加功能
    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public R save( @RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if(save){
            return R.ok();
        }{
            return R.error();
        }
    }

    //2 删除功能
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")
    public R removeById(@ApiParam(name="id",value="讲师ID",required = true) @PathVariable String id){
        return eduTeacherService.removeById(id)?R.ok():R.error();
    }

    //3  修改回显功能 点修改跳转页面 根据id查询功能 根据id查询 回显功能由前端配合完成
    @ApiOperation(value = "id查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@ApiParam(name="id",value="讲师ID",required = true)@PathVariable String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("eduTeacher",eduTeacher);
    }

    //3.1 修改保存功能
    @ApiOperation(value = "修改讲师")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        return eduTeacherService.updateById(eduTeacher)?R.ok():R.error();
    }

    //rest风格
    // 4 查询讲师表 所有数据
    @ApiOperation(value = "讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }

    //4.1 分页显示
    @ApiOperation(value = "分页显示讲师")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@ApiParam(name="current",value="当前页",required = true)@PathVariable long current,
                             @ApiParam(name="limit",value="当前页数据量",required = true)@PathVariable long limit){
        //创建page对象
        Page<EduTeacher> pageTeacher=new Page<EduTeacher>(current,limit);
        try{
            int i=10/0;
        }catch(Exception e){
            throw new GuliException(20001,"执行了自定义异常处理");
        }

        eduTeacherService.page(pageTeacher,null);
        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords();//每页数据
        Map map=new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);
    }

    // 4.2 列表功能 条件查询分页显示
    @ApiOperation(value = "条件查询分页显示讲师")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageQuery(@ApiParam(name="current",value="当前页",required = true)@PathVariable long current,
                       @ApiParam(name="limit",value="当前页数据量",required = true)@PathVariable long limit,
                       @ApiParam(name="teacherQuery",value="输入条件",required = false)@RequestBody TeacherQuery teacherQuery){
        //创建page对象
        Page<EduTeacher> pageTeacher=new Page<EduTeacher>(current,limit);
        //构建条件
        QueryWrapper<EduTeacher> wrapper=new QueryWrapper<EduTeacher>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_modified",end);
        }
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        eduTeacherService.page(pageTeacher,wrapper);
        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords();//每页数据
        Map map=new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);
    }

}

